#include "commonmessage.h"

#include <QList>

CommonMessage::CommonMessage(MesType type, QString msg) {
    this->type = type;
    this->timestamp = QDateTime::currentMSecsSinceEpoch();
    this->msg = msg;
}

CommonMessage::CommonMessage(MesType type, qint64 timestamp, QString msg) {
    this->type = type;
    this->timestamp = timestamp;
    this->msg = msg;
}

QString CommonMessage::messageToString(CommonMessage msg){
    return QString::number(msg.type)+"\n"+QString::number(msg.timestamp)+"\n"+msg.msg+"\n";
};

QList<CommonMessage> CommonMessage::stringToMessage(QString str){
    QList<QString> lst = QList(str.split("\n"));
    QList<CommonMessage> msgs;
    for(int i=0; i<=lst.size()-3; i+=3){
        msgs.append(CommonMessage((MesType)(lst[i].toInt()), lst[i+1].toLongLong(), lst[i+2]));
    }

    return msgs;
};
